-- tips提供的操作按钮

local btnHeight = 56 -- 按钮高度

local newButtonTipsView = class("newButtonTipsView",function()
    return ccui.Layout:create()
end)

function newButtonTipsView:ctor()
    self.mUINode = cc.uiloader:load( Res.Tips_Button )
    self:addChild( self.mUINode )

    self.moreLayer = self.mUINode:getChildByName("ListView")
    self.moreLayer:setPositionY( 0 )
    local listViewContentSize = self.moreLayer:getContentSize()
    self.mUINode:setContentSize( listViewContentSize )
    self.mUINode:setPositionY( 0 )
    self:setContentSize( listViewContentSize )
    ccui.Helper:doLayout( self.mUINode )
end

-- 帮助函数：创建tips操作按钮
local function createBtnNode( name, callback, btnNodeName )
    local itemUI = cc.uiloader:load( Res.Tips_ButtonNode )

    local panel
    if btnNodeName == "Button_decompose" then   --分解按钮特殊处理
        panel = itemUI:getChildByName("Panel_2")
        panel:setVisible(true)
    else
        panel = itemUI:getChildByName("Panel")
    end
    panel:setName("panel")
    panel:retain()
    panel:removeFromParent()

    local itemNode = panel:getChildByName("button")
    itemNode:setVisible(false)
    if btnNodeName == "Button_equip" then
        itemNode = panel:getChildByName("button_y")
        itemNode:setVisible(true)
    elseif btnNodeName == "Button_unequip" then
        itemNode = panel:getChildByName("button_r")
        itemNode:setVisible(true)
    else
        itemNode = panel:getChildByName("button")
        itemNode:setVisible(true)
    end

    local nameLabel = panel:getChildByName("name")
    nameLabel:setString(name)

    itemNode:addTouchEventListener(callback)

    local default_item = ccui.Layout:create()
    default_item:setTouchEnabled(true)
    default_item:setContentSize(panel:getContentSize())
    default_item:addChild(panel)
    panel:release()

    return default_item
end

function newButtonTipsView:refreshContent( btnList )
    -- dump(btnList," newButtonTipsView refreshContent params")
    self.moreLayer:removeAllChildren()
    local Button_decompose = self.mUINode:getChildByName("Button_decompose")
    if Button_decompose then
        Button_decompose:setVisible(false)
    end

    if #btnList > 0 then
        self.moreLayer:setVisible( true )
    end

    local function sortFunc( lhs, rhs )
        local lhs_funcId = lhs.btnFuncId
        local rhs_funcId = rhs.btnFuncId
        if lhs_funcId and rhs_funcId then
            local lhs_featureonConf = GD:queryFeatureOnById( lhs_funcId )
            local rhs_featureonConf = GD:queryFeatureOnById( rhs_funcId )
            if tonumber( lhs_featureonConf.btnPos ) == tonumber( rhs_featureonConf.btnPos ) then
                return false
            else
                return tonumber( lhs_featureonConf.btnPos ) < tonumber( rhs_featureonConf.btnPos )
            end
        elseif lhs_funcId then
            return true
        elseif rhs_funcId then
            return false
        else
            return true
        end
    end
    table.sort( btnList, sortFunc )

    local btnNum = #btnList
    for i = 1, btnNum do
        local v = btnList[i]
        if v.callback then
            local function touchEvent(sender,eventType)
                if eventType == ccui.TouchEventType.ended then
                    if v.callback then
                        v.callback()
                    end
                end
            end
            local btn = createBtnNode( v.btnName, touchEvent, v.btnNodeName )
            if v.btnNodeName then
                btn:setName( v.btnNodeName )
            end
            btn:setTag( btnNum )
            if v and v.btnFuncId then -- 存在功能Id
                local featureonConf = GD:queryFeatureOnById( v.btnFuncId )
                if featureonConf and featureonConf.btnTips and "" ~= featureonConf.btnTips then
                    local iconPath = GD:getImagePath( Res.tipsIconPath, featureonConf.btnTips )
                    if iconPath and "" ~= iconPath then
                        btn:findChild( "panel/icon" ):loadTexture( iconPath )
                    end
                end
            end
            if v.icon then
                local iconPath = GD:getImagePath( Res.tipsIconPath, v.icon )
                btn:findChild( "panel/icon" ):loadTexture( iconPath )
            end

            btnNum = btnNum -1

            if v.btnNodeName == "Button_decompose" then
                local tempBtn = self.mUINode:getChildByName("Button_decompose")
                if not tempBtn then
                    self.mUINode:addChild(btn)
--                    btn:setVisible(true)
                else
                    tempBtn:setVisible(true)
                end
            else
                self.moreLayer:addChild(btn)
            end
        end
    end

    self.moreLayer:refreshView()
    local listSize = cc.size( self.moreLayer:getContentSize().width, btnHeight * #btnList )
    self.moreLayer:setContentSize( listSize )

    self.mUINode:setContentSize( listSize )
    self:setContentSize( listSize )

    ccui.Helper:doLayout( self.moreLayer )
end

function newButtonTipsView:adjust(width, height)
    local btn = self.mUINode:getChildByName("Button_decompose")
    if btn then
        btn:setPosition(width - self.moreLayer:getContentSize().width, -height)
    end
end

-- 专为guide服务，根据btn名字返回按钮
function newButtonTipsView:getBtnByName( btnName )
    local btn
    if btnName == "Button_decompose" then
        btn = self.mUINode:getChildByName("Button_decompose")
    else
        btn = self.moreLayer:getChildByName(btnName)
    end
    if btn then
        return btn:getChildByName("panel")
    end
end

return newButtonTipsView
